<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="68px"
      @submit.native.prevent
    >
      <el-form-item label="备份表" prop="tables">
        <el-input
          v-model="queryParams.tables"
          placeholder="请输入备份表"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openImportTable"
          v-hasPermi="['system:backups:add']"
        >
          备份
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:backups:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <File-upload
          :is-show-tip="false"
          :file-type="['sql']"
          :multiple="false"
          show-msg
          icon="el-icon-upload"
          action-url="/tool/backupRecord/uploadFileRecover"
          text="导入本地备份"
          v-hasPermi="['system:backups:import']"
        />
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:backups:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" type="index" width="80" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备份表" align="center" prop="tables" />
      <el-table-column label="备份类型" align="center" prop="backupType" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.backupType == 1">指定范围</span>
          <span v-if="scope.row.backupType == 2">全部备份</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="备份日期" align="center" prop="createTime" width="180" />
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
            v-hasPermi="['system:backups:download']"
          >
            下载备份
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="handleRestore(scope.row)"
            v-hasPermi="['system:backups:restore']"
          >
            还原
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:backups:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <import-table ref="import" @ok="handleQuery" />
  </div>
</template>

<script>
import {
  listBackupRecord,
  delBackupRecord,
  postBackupRestore,
} from "@/api/tool/backupRecord";
import importTable from "./importTable";

export default {
  name: "BackupRecord",
  components: { importTable },
  data() {
    return {
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: false, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      dataList: [], // 表格数据
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tables: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询数据备份记录列表
    getList() {
      this.loading = true;
      listBackupRecord(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 打开备份
    openImportTable() {
      this.$refs.import.show();
    },
    // 下载备份
    handleDownload(row) {
      this.$download.backup(row.id);
    },
    // 上传本地备份
    handleImport() {},
    // 还原备份
    handleRestore(row) {
      this.$modal
        .confirm("是否确认还原数据到此备份版本？")
        .then(() => {
          postBackupRestore({ id: row.id }).then(() => {
            this.getList();
            this.$modal.msgSuccess("还原成功");
          });
        })
        .catch(() => {});
    },
    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 删除
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除数据备份记录编号为"' + ids + '"的数据项？')
        .then(() => {
          delBackupRecord(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
    // 导出
    handleExport() {
      this.download(
        "tool/backupRecord/export",
        {
          ...this.queryParams,
        },
        `backupRecord_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
